<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
 
/**
 * Product Controller
 */
class ProductControllerCheckout extends JControllerForm
{
	protected $mail_body;
	public function submit() {
		$success = true;
		
		$name = trim(JRequest::getString('name', '', 'POST'));
		$phone = trim(JRequest::getString('phone', '', 'POST'));
		$mail = trim(JRequest::getString('email', '', 'POST'));
		$address = trim(JRequest::getString('address', '', 'POST'));
		
		$model = $this->getModel('Cart', 'ProductModel');
		$cart = $model->getItems();
		
		if ($error = $this->canSubmit($name, $phone, $mail, $address, $cart) !== true) {
			$this->setError($error);
			$success = false;
		}
		
		$more_info = trim(JRequest::getString('more_info', '', 'POST'));

		if ($success && $body =& $this->createMailBody($name, $phone, $mail, $address, $more_info, $cart) === false) {
			$this->setError("can't create mail body");
			$success = false;
		}
		
		if ($success && $this->mailToSystem($body) !== true) {
			$this->setError("can't send mail");
			$success = false;
		}
		
		if ($success) {
			$session = JFactory::getSession();
			//$session->clear('cart');
			$this->setRedirect('index.php?option=com_product&view=checkout&layout=success');
			return true;
		} else {
			JRequest::setVar('view', 'checkout');
			$this->display();
			return false;
		}
	}
	
	private function canSubmit($name, $phone, $mail, $address, $cart) {
		if (!isset($name) || !isset($phone) || !isset($mail) || !isset($address)) {
			return "Lỗi 1";
		}
		
		if (strlen($name) == 0 || strlen($phone) == 0 || strlen($mail) == 0 || strlen($address) == 0) {
			return "Lỗi 2";
		}
		
		if (!isset($cart) || !is_array($cart)) {
			return "Lỗi 3";
		}
		
		return true;
	}
	
	private function createMailBody($name, $phone, $mail, $address, $more_info, $cart) {
		if (!isset($this->mail_body)) {
			if ($this->mail_body =& file_get_contents(JURI::root() .'components/com_product/template/cartEmailTmp.txt', FILE_USE_INCLUDE_PATH) === false) {
				return false;
			}
		}
		$body = $this->mail_body;
		$body = str_replace('{@name}', $name, $body);
		$body = str_replace('{@phone}', $phone, $body);
		$body = str_replace('{@email}', $mail, $body);
		$body = str_replace('{@address}', $address, $body);
		$body = str_replace('{@more_info}', $more_info, $body);
		$detail = '';
		foreach ($cart as $item) {
			$detail .= ' - ' . $item['data']->code . ' x ' . $item['qty'] . '<br />';
		}
		$body = str_replace('{@cart_detail}', $detail, $body);
		return $body;
	}
	
	private function mailToSystem($body) {
		$mailer =& JFactory::getMailer();
		
		// Setting sender 
		$config =& JFactory::getConfig();
		$sender = array($config->getValue('config.mailfrom'), $config->getValue('config.fromname'));
		$mailer->setSender($sender);
		
		// Setting recipient
		$mailer->addRecipient(array('vtbminh@hotmail.com'));
		
		// Creating the mail
		$mailer->setSubject('[Important!!!!!] New Order');
		$mailer->isHTML(true);
		$mailer->Encoding = 'base64';
		$mailer->setBody($body);
		
		return $mailer->Send();
	}
}